//
//  MyWorkTableView.m
//  WULIHH
//
//  Created by chen tang on 16/4/27.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "MyWorkTableView.h"
#import "Header.h"
#import "MyWorkTableViewCell.h"

#import "MJRefresh.h"

#import "WuLiUserWorkReauest.h"
#import "UserInfoModel.h"

@interface MyWorkTableView ()<UITableViewDataSource, UITableViewDelegate, MyWorkTableViewCellDelegate>
{
    UITableView *mainTableView;
    NSMutableArray *dataMUtableArray;
    NSString *pageString;
}

@end


@implementation MyWorkTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUserInteractionEnabled:YES];
        pageString = @"1";
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userWorkListGetBack:) name:workList object:nil];
        
        dataMUtableArray = [NSMutableArray new];
        
        mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WithView, heightView-184-49)];
        [self addSubview:mainTableView];
        [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [mainTableView setDataSource:self];
        [mainTableView setDelegate:self];
        
        mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadMyWorkList)];
    }
    return self;
}

#pragma mark---用户作品
- (void)userWorkListGetBack:(NSNotification *)notion
{
    [mainTableView.mj_header endRefreshing];
    [mainTableView.mj_footer endRefreshing];
    
    NSDictionary *dic = [notion userInfo];
    //    NSLog(@"%@", dic);
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        if ([pageString isEqualToString:@"1"])
        {
            if ([[dic valueForKey:@"data"] count] == 0)
            {
                [dataMUtableArray removeAllObjects];
            }
            else
            {
                [dataMUtableArray removeAllObjects];
                [dataMUtableArray addObjectsFromArray:[dic valueForKey:@"data"]];
            }
        }
        else
        {
            if ([[dic valueForKey:@"data"] count] == 0)
            {
                pageString = [NSString stringWithFormat:@"%d", [pageString intValue] - 1];
            }
            [dataMUtableArray addObjectsFromArray:[dic valueForKey:@"data"]];
            
        }
        
        [mainTableView reloadData];
    }
    else
    {
        
    }
}

- (void)loadMyWorkList
{
    pageString = @"1";
    [WuLiUserWorkReauest requestUserWorkList:[UserInfoModel userIDString] withPage:pageString];

}

- (void)beginRefresh
{
    [mainTableView.mj_header beginRefreshing];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"%d", dataMUtableArray.count);
    return dataMUtableArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 113;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    
    MyWorkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell) {
        cell = [[MyWorkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setModelWithMyWork:dataMUtableArray[indexPath.row]];
    [cell setTag:indexPath.row];
    [cell setMMyWorkTableViewCellDelegate:self];
    return cell;
}

- (void)pushWithTag:(int)tag
{
    [_MMyWorkTableViewDelegate pushWorkDetal:[NSString stringWithFormat:@"%d", tag] withDic:dataMUtableArray[tag]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"1111");
    
    if (_MMyWorkTableViewDelegate && [_MMyWorkTableViewDelegate respondsToSelector:@selector(pushWorkDetal:withDic:)]) {
        [_MMyWorkTableViewDelegate pushWorkDetal:[NSString stringWithFormat:@"%d", (int)indexPath.row] withDic:dataMUtableArray[indexPath.row]];
//        NSLog(@"%@", dataMUtableArray[indexPath.row]);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
